package org.tgcloud.group.service.manager;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.tgcloud.group.core.*;
import org.tgcloud.group.core.ExportUtils.ExcelUtils;
import org.tgcloud.group.entity.*;
import org.tgcloud.group.repository.MjBusinessPersonMapper;
import org.tgcloud.group.repository.MjProjectMapper;
import org.tgcloud.group.repository.SysLabelValueMapper;
import org.tgcloud.group.repository.SysManagerUserMapper;
import org.tgcloud.group.web.controller.dto.file.ProjectHistoryDTO;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author: zhy
 * @create: 2023-10-13 17:16
 * @description:
 */
@Slf4j
@Service
public class ProjectHistoryService {

    @Autowired
    private MjProjectMapper projectMapper;

    @Autowired
    private SysLabelValueMapper sysLabelValueMapper;

    @Autowired
    private MjBusinessPersonMapper businessPersonMapper;

    @Autowired
    private SysManagerUserMapper managerUserMapper;

    @Autowired
    private PasswordHelper passwordHelper;

    public APPResultMessage doImportExcelProjectHistory(MultipartFile file) {
        try {
            String fileName = StringUtil.generateOrderIdByName("CHECK");
            List<ProjectHistoryDTO> dtos = ExcelUtils.readMultipartFile(file, ProjectHistoryDTO.class, fileName,null,null);
            for (ProjectHistoryDTO dto : dtos){
                MjProject project = new MjProject();
                project.setProjectName(dto.getBrand());
                project.setBrands(dto.getBrand());
                // 区域
                SysLabelValue sysLabelValue3 = sysLabelValueMapper.selectValueBySysIdAndName(3, dto.getAreaName());
                project.setAreaId(sysLabelValue3.getId().toString());
                // 品类
                SysLabelValue sysLabelValue2 = sysLabelValueMapper.selectValueBySysIdAndName(2, dto.getProductType());
                Integer sysLabelValue2Id = null;
                if (sysLabelValue2 == null){
                    SysLabelValue sysLabelValue = new SysLabelValue();
                    sysLabelValue.setSysLabelId(2);
                    sysLabelValue.setCreateTime(new Date());
                    sysLabelValue.setStatus((short) 1);
                    sysLabelValue.setSysValueName(dto.getProductType());
                    sysLabelValueMapper.insertSelective(sysLabelValue);
                    sysLabelValue2Id = sysLabelValue.getId();
                }else {
                    sysLabelValue2Id = sysLabelValue2.getId();
                }
                project.setProductName(dto.getProductType());
                project.setCateIds(sysLabelValue2Id.toString());
                project.setActualValuePrice(dto.getTotalPrice());

                //商务
                MjBusinessPerson businessPerson = businessPersonMapper.selectMsgByUsername(dto.getBusinessName());
                Integer businessId = null;
                if (businessPerson == null){
                    MjBusinessPerson person = new MjBusinessPerson();
                    person.setUsername(dto.getBusinessName());
                    person.setCreateTime(new Date());
                    person.setStatus((short) 2);
                    person.setArea(dto.getAreaName());
                    businessPersonMapper.insertSelective(person);
                    businessId = person.getId();
                }else {
                    businessId = businessPerson.getId();
                }
                project.setBussinessUsername(dto.getBusinessName());
                project.setBusinessId(businessId);

                // 执行负责人
                Integer managerUserId = null;
                if (StringUtil.isNotBlank(dto.getProjectUserName())){
                    SysManagerUser managerUser = managerUserMapper.selectByUsername(dto.getProjectUserName());
                    if (managerUser == null){
                        SysManagerUser user = new SysManagerUser();
                        user.setPassword("123456");
                        user.setUsername(dto.getProjectUserName());
                        user.setAccount(dto.getProjectUserName());
                        user.setRoleId("50");
                        user.setStatus((short) 1);
                        passwordHelper.encryptPasswordManagerUser(user);
                        managerUserMapper.insertSelective(user);
                        managerUserId = user.getId();
                    }else {
                        managerUserId = managerUser.getId();
                    }
                    project.setPermissionId(managerUserId);
                }
                project.setStatus((short) 1);
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
                project.setCreateTime(sdf.parse(dto.getShowTime()));
                projectMapper.insertSelective(project);
            }
            APPResultMessage successMessage = APPResultMessage.getSuccessMessage();
            ResultMessage resultMessage = ResultMessage.builder().setList(dtos);
            successMessage.setResult(resultMessage);
            return successMessage;
        }catch (Exception e){
            log.error(e.getMessage());
            e.printStackTrace();
            return APPResultMessage.getErrorMessage(e.getMessage());
        }
    }


}
